package com.dz.xjd.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import com.alibaba.fastjson.JSON;
import com.dz.xjd.R;
import com.dz.xjd.api.TokenManager;
import com.dz.xjd.app.App;
import com.dz.xjd.app.Constant;
import com.dz.xjd.bean.LoginParams;
import com.dz.xjd.bean.MsmParams;
import com.dz.xjd.params.Params;
import com.dz.xjd.ui.base.BaseActivity;
import com.dz.xjd.ui.contract.RegisterContract;
import com.dz.xjd.ui.model.RegisterModel;
import com.dz.xjd.ui.presenter.RegisterPresenter;
import com.dz.xjd.utils.EncryptDecrypt;
import com.dz.xjd.utils.KeyUtil;
import com.dz.xjd.utils.ToastUtil;
import com.orhanobut.logger.Logger;
import butterknife.BindView;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity <RegisterPresenter, RegisterModel> implements RegisterContract.View{

    @BindView(R.id.layout_top_tv_left)
    TextView mTvTitleBack;

    @BindView(R.id.layout_top_tv_title)
    TextView mTvtitle;

    @BindView(R.id.et_register_username)
    EditText mEtUsername;

    @BindView(R.id.et_register_validate)
    EditText mEtValidate;

    @BindView(R.id.btn_register_validate)
    Button mBtnValidate;

    @BindView(R.id.btn_register_register)
    Button mBtnRegister;

    @BindView(R.id.cb_register_read)
    CheckBox mCbRead;

    private String username;
    /*private String password;*/
    private String validate;

    @Override
    protected int getLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void init() {
        mTvtitle.setText("注册");
        mCbRead.setOnCheckedChangeListener(mOnCheckedChangeListener);
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public void returnRegisterData() {

    }

    @OnClick({R.id.btn_register_validate, R.id.btn_register_register,R.id.layout_top_tv_left})
    void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_top_tv_left :
                onBackPressed();
                break;

            case R.id.btn_register_validate :
                validateSubmit();
                break;

            case R.id.btn_register_register :
                registerSubmit();
                break;
        }

    }

    private void validateSubmit() {
        if(verifyLoginInfo()) {
            String header = Params.normalHeadParams(Constant.METHOD_SMS);

            MsmParams msmParams = new MsmParams();
            msmParams.setUser_id(0);
            msmParams.setPhone(username);
            msmParams.setType(0);
            String params = JSON.toJSONString(msmParams);
            String[] keys = KeyUtil.getKey(App.getToken());
            Logger.d(params);
            String str = "";
            try {
                str = EncryptDecrypt.encrypt(keys[0], params, keys[1]);
                Logger.d(params);
            } catch (Exception e) {
                e.printStackTrace();
            }
            mPresenter.sendValidateRequest(header, str);
        }
    }

    private void registerSubmit() {
        if(verifyLoginInfo()){
            String header = Params.normalHeadParams(Constant.METHOD_LOGIN);

            LoginParams loginParams = new LoginParams();
            loginParams.setUser_id(0);
            loginParams.setPhone(username);
            loginParams.setCode(validate);
            String params = JSON.toJSONString(loginParams);
            Logger.d(params);
            String[] keys = KeyUtil.getKey(App.getToken());
            String str = "";
            try {
                str = EncryptDecrypt.encrypt(keys[0],params,keys[1]);
                Logger.d(str);
            } catch (Exception e) {
                e.printStackTrace();
            }
            mPresenter.getRegisterRequest(header, str);
        }

    }

    private boolean verifyLoginInfo() {
        username = mEtUsername.getText().toString().trim();
       /* password = mEtPassword.getText().toString().trim();*/
        validate = mEtValidate.getText().toString().trim();
        if(TextUtils.isEmpty(username)){
            ToastUtil.showShort("请输入用户名");
            return false;
        }

        if(TextUtils.isEmpty(validate)){
            ToastUtil.showShort("请输入验证码");
            return false;
        }

        return true;
    }

    private CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                mBtnRegister.setEnabled(true);
            }else{
                mBtnRegister.setEnabled(false);
            }
        }
    };


    private TokenManager.onTokenChangeListener validaTokenChange = new TokenManager.onTokenChangeListener(){
        public void tokenChanged(String token){
            validateSubmit();
        }
    };

    private TokenManager.onTokenChangeListener registerTokenChange = new TokenManager.onTokenChangeListener(){
        public void tokenChanged(String token){
            registerSubmit();
        }
    };
}
